package com.elitel.common.auth.sysresource.service.impl;

import com.elitel.common.auth.sysresource.cache.SysResourceCache;
import com.elitel.common.auth.sysresource.entity.SysResource;
import com.elitel.common.auth.sysresource.service.SysResourceService;
import com.elitel.common.utils.SysUserUtils;
import com.elitel.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述
 *
 * @author: mls
 */
@Service("defMenuService")
public class SysResourceServiceImpl implements SysResourceService {
    private static final Logger logger = LoggerFactory.getLogger(SysResourceServiceImpl.class);

    /**
     * @return
     */
    @Override
    public List<SysResource> getMenuList() {
        String roleIds = SysUserUtils.getRoleIds();
        if (StringUtils.isEmpty(roleIds)) {
            logger.error("获取用户角色为空");
            return null;
        }
        String[] split = roleIds.split(",");
        ArrayList<SysResource> res = new ArrayList<>();
        for (String roleId : split) {
            List<SysResource> sysResourceList = SysResourceCache.getMenuList(roleId);
            res.addAll(sysResourceList);
        }
        return res.stream().distinct().collect(Collectors.toList());
    }

    @Override
    public void updateMenuList(){
        SysResourceCache.updateCache();
    }

}
